# plugs/update.py
#
#

""" run mercurial pull -u """

__copyright__ = 'this file is in the public domain'

from gozerbot.commands import cmnds
from gozerbot.examples import examples
from gozerbot.generic import gozerpopennocheck
from gozerbot.plughelp import plughelp
import os, os.path

plughelp.add('update', 'do a mercurial update')

def handle_hgupdate(bot, ievent):
    """ update .. update the bot with mercurial hg pull -u """
    if not os.path.isdir('.hg'):
        ievent.reply('run "hg clone http://r8.cg.nu/hg/gozerbot" to get \
a mercurial version of the bot .. see doc/UPGRADE')
        return
    # execute hg pull -u
    args = [""] * 3
    args[0] = "hg"
    args[1] = "pull"
    args[2] = "-u"
    try:
        proces = gozerpopennocheck(args)
    except Exception, ex:
        ievent.reply('error running popen: %s' % str(ex))
        return
    data = proces.fromchild.readlines()
    if proces.wait():
        ievent.reply("no changes")
        return
    # show response
    res = []
    for line in data:
        res.append(line.strip())
    if res:
        ievent.reply(" .. ".join(res))

cmnds.add('update', handle_hgupdate, 'OPER')
examples.add('update', 'update the bot with mercurial', 'update')
